package com.crm.utils;

import com.crm.constant.enums.FileTypeEnum;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version 1.0
 * @author: CRM_SYSTEM
 * @date: 2022/3/31 19:50
 * @Description:
 */
public class CrmFileUtils {

    /**
     * 文件名后缀前添加一个时间戳
     */
    public static String getFileName(String fileName) {
        int index = fileName.lastIndexOf(".");
        final SimpleDateFormat sDateFormate = new SimpleDateFormat("yyyymmddHHmmss");  //设置时间格式
        String nowTimeStr = sDateFormate.format(new Date()); // 当前时间
        fileName = fileName.substring(0, index) + "_" + nowTimeStr + fileName.substring(index);
        return fileName;
    }

    public static String getUploadPath(FileTypeEnum fileTypeEnum,String fileName){
        File path=null;
        try {
            path=new File(ResourceUtils.getURL("src/main/resources").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (!path.exists()){
            new File("");
        }
        String pathLoad = "static/upload/" + fileTypeEnum.getValue();
        File upload = new File(path.getAbsolutePath(),pathLoad );
        if (!upload.exists()) upload.mkdirs();
        return upload.getAbsolutePath();
    }

    public static void main(String[] args) throws FileNotFoundException {
        URL url = ResourceUtils.getURL("src/");
        System.out.println(url);
    }
}
